const app = getApp();
Component({
    /**
     * 组件的属性列表
     */
    properties: {
        role: {
            type: String,
            value: ""
        },
        roleType: {
            type: String,
            value: "",
        },
    },

    /**
     * 组件的初始数据
     */
    data: {
        userDetail: null,
        searchValue: "",
        allBusinessList: [],
        totalObj: {},
        statusMap: {
            1: "待除皮",
            2: "待开价",
            3: "待结算",
            4: "已结算"
        },
        statusMapColor: {
            1: "#69B657",
            2: "#FFB037",
            3: "#E21C1C",
            4: "#666666"
        },
        isLastPage: false,
        current: 1,
        size: 10,
    },

    lifetimes: {
        attached: function () {
            // 在组件实例进入页面节点树时执行
            this.setData({
                userDetail: app.globalData.userInfo.userDetail
            })
            this.getData()
        },
        detached: function () {
            // 在组件实例被从页面节点树移除时执行
        },
    },

    /**
     * 组件的方法列表
     */
    methods: {
        onPullDownRefresh() {
            this.setData({
                current: 1,
                searchValue: "",
                allBusinessList: [],
                isLastPage: false
            })
            this.getData()
        },
        onReachBottom() {
            let {
                current,
                isLastPage
            } = this.data
            if (isLastPage) {
                return
            }
            this.setData({
                current: (Number(current) + 1)
            })
            this.getData()
        },
        onSearch() {
            this.setData({
                current: 1,
                allBusinessList: [],
                isLastPage: false
            })
            this.getData()
        },
        getrow(e) {
            const item = e.currentTarget.dataset.item
            let name = item.name,
                id = item.userId,
                mobile = item.mobile,
                type;
            if (this.data.roleType == 'purchaser') {
                type = 1
            } else {
                type = 2
            }
            wx.navigateTo({
                url: `/pages/dashboard/analysis/analysis?userId=${id}&name=${name}&type=${type}&mobile=${mobile}`
            });
        },
        toDetail(e) {
            let item = e.currentTarget.dataset.item
            wx.navigateTo({
                url: `/pages/business/details/index?id=${item.id}`,
            })
        },
        syncToAccountBook(e) {
            let item = e.currentTarget.dataset.item
            item.orderType = 2
            item.type = 0
            item.financeName = "卖菜"
            this.triggerEvent('bookPicker', item)
        },
        getTotal() {
            wx.$apis.getFarmerReport().then(res => {
                if (res.data) {
                    this.setData({
                        totalObj: res.data,
                        "totalObj.goodsPrice": wx.$getYuan(res.data.goodsPrice)
                    })
                }
            }).catch(err => {
                wx.stopPullDownRefresh();
            })
        },
        getData() {
            const {
                role,
                current,
                roleType,
                size
            } = this.data;
            let api,
                params = {
                    current,
                    size,
                }

            if (role == 2) {
                api = "getFarmerList"
                this.getTotal();
            } else {
                if (roleType == "purchaser") {
                    api = "shipperTradeOrderPage"
                } else {
                    api = "farmerTradeOrderPage"
                }
            }
            if (role != 2) {
                params.keyword = this.data.searchValue;
                params.companyId = app.globalData.userInfo.companyData.companyId
            }
            wx.showLoading({
                title: "正在请求数据中"
            })
            wx.$apis[api](params).then(res => {
                wx.stopPullDownRefresh();
                let oldAllBusinessList = this.data.allBusinessList;
                let allBusinessList = res.data && res.data.records.map(item => {
                    item.averagePrice = item.averagePrice && wx.$getYuan(item.averagePrice)
                    item.countPrice = item.countPrice && wx.$getYuan(item.countPrice)
                    item.managementFee = item.managementFee && wx.$getYuan(item.managementFee)
                    item.transactionPrice = item.transactionPrice && wx.$getYuan(item.transactionPrice)
                    item.createTimeStr = item.createTime && item.createTime.replace("T", " ")
                    return item
                })
                if (allBusinessList && allBusinessList.length) {
                    let newAllBusinessList = oldAllBusinessList.concat(allBusinessList)
                    this.setData({
                        allBusinessList: newAllBusinessList
                    })
                    if (allBusinessList.length < size) {
                        this.setData({
                            isLastPage: true
                        })
                    }
                } else {
                    this.setData({
                        isLastPage: true
                    })
                }
            }).catch(err => {
                wx.stopPullDownRefresh();
            })
        }
    }
})